package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Goods;
import com.etc.util.AjaxUploadResponse;
import com.etc.util.PageData;
import com.etc.util.ResponseData;
import com.etc.zmall.service.GoodsService;
import com.etc.zmall.service.impl.GoodsServiceImpl;
import com.google.gson.Gson;

/**
 * 
 * <p>
 * Title: AjaxGoodsServlet
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno 明禄
 * 
 * @date 2021年1月11日
 * 
 */
@WebServlet("/ajaxgoods.do")
public class AjaxGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 调用service
	GoodsService gs = new GoodsServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 最先做的事情，设置请求编码的格式
		request.setCharacterEncoding("utf-8");
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("add".equals(op)) {
			// 增加操作
			doAdd(request, response);
		} else if ("querybyid".equals(op)) {
			// 查询
			doQueryById(request, response);
		} else {
			// 查询
			doQuery(request, response);
		}

	}

	protected void doQueryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int goodsid = 0;
		if (request.getParameter("goodsid") != null) {
			goodsid = Integer.parseInt(request.getParameter("goodsid"));
		}
		System.out.println("doQueryById :" + goodsid);
		// MIME,返回类型就是json => application/json
		response.setContentType("application/json;charset=utf-8");

		PrintWriter out = response.getWriter();

		// keywords就是传递的查询的关键字
		Goods goods = gs.queryOneGoods(goodsid);	
		Gson gson = new Gson();
		String str = gson.toJson(goods);

		System.out.println("str: " + str);

		out.print(str);
		// out.append(str);

		out.close();

	}

	protected void doAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// MIME,返回类型就是json => application/json
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		// 接收表单提交的数据，获取name="keywords"的值,
		String cover = "";
		if (request.getParameter("cover") != null) {
			cover = request.getParameter("cover");
		}
		int goodscount = 0;
		if (request.getParameter("goodscount") != null) {
			goodscount = Integer.parseInt(request.getParameter("goodscount"));
		}
		String goodsinfo = "";
		if (request.getParameter("goodsinfo") != null) {
			goodsinfo = request.getParameter("goodsinfo");
		}
		String goodsname = "";
		if (request.getParameter("goodsname") != null) {
			goodsname = request.getParameter("goodsname");
		}

		double goodsprice = 0;
		if (request.getParameter("goodsprice") != null) {
			goodsprice = Double.parseDouble(request.getParameter("goodsprice"));
		}
		int typeid = 1;// 假数据
		int shopid = 1;// 假数据
		int goodsstate = 0;

		Goods goods = new Goods(goodsname, goodsprice, goodsinfo, goodscount, cover, typeid, shopid, goodsstate);
		System.out.println("goods: " + goods);

		boolean flag = gs.addGoods(goods);

		AjaxUploadResponse ar = new AjaxUploadResponse();
		ar.setCode(0);
		if (flag) {
			ar.setMsg("操作成功");
		} else {
			ar.setMsg("操作失败");
		}
		Gson gson = new Gson();
		String str = gson.toJson(ar);
		out.print(str);
		out.close();

	}

	/**
	 * 查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// MIME,返回类型就是json => application/json
		response.setContentType("application/json;charset=utf-8");

		PrintWriter out = response.getWriter();
		// 接收表单提交的数据，获取name="keywords"的值,
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}

		int pageNo = 1;
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			pageNo = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("limit") != null) {
			pageSize = Integer.parseInt(request.getParameter("limit"));
		}

		System.out.println("keywords :" + keywords);

		// keywords就是传递的查询的关键字
		PageData<Goods> pd = gs.showGoods(pageNo, pageSize, keywords);

		// pd.getTotalCount(), 总记录数

		// pd.getList() 页面的数据
		ResponseData<Goods> ar = new ResponseData<Goods>(0, "success", pd.getTotalCount(), pd.getList());

		Gson gson = new Gson();
		String str = gson.toJson(ar);

		System.out.println("str: " + str);

		out.print(str);

		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
